package com.yiwei.zxp.view;

import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.zxp.Application;
import com.yiwei.zxp.R;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.util.PreferenceUtils;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class PacketPayStepDilaog extends Dialog {


    @BindView(R.id.btn_cancel)
    ImageView btnCancel;
    @BindView(R.id.txt_info_one)
    TextView txtInfoOne;
    @BindView(R.id.btn_one_copy)
    SuperButton btnOneCopy;
    @BindView(R.id.txt_info_two)
    TextView txtInfoTwo;
    @BindView(R.id.btn_two_copy)
    SuperButton btnTwoCopy;
    @BindView(R.id.btn_three_copy)
    SuperButton btnThreeCopy;
    @BindView(R.id.txt_info_three)
    TextView txtInfoThree;
    @BindView(R.id.btn_phone)
    TextView btnPhone;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Context context;
    private ClipboardManager cm;

    public PacketPayStepDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public PacketPayStepDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_packet_pay_step);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        mWindow.setAttributes(lp);
        cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        btnPhone.setText(PreferenceUtils.getString(Application.getInstance(), "service_phone", ""));
        txtInfoOne.setText(PreferenceUtils.getString(Application.getInstance(), "bankcard_no", ""));
        txtInfoTwo.setText(PreferenceUtils.getString(Application.getInstance(), "name", ""));
        txtInfoThree.setText(PreferenceUtils.getString(Application.getInstance(), "bankzone", ""));
    }


    @OnClick({R.id.btn_cancel, R.id.btn_one_copy, R.id.btn_two_copy, R.id.btn_three_copy, R.id.btn_phone, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_one_copy:
                // 创建普通字符型ClipData
                ClipData mClipData1 = ClipData.newPlainText("Label", UI.toString(txtInfoOne));
// 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData1);
                ToastUtil.makeText(context, "复制成功");
                break;
            case R.id.btn_two_copy:
                // 创建普通字符型ClipData
                ClipData mClipData2 = ClipData.newPlainText("Label", UI.toString(txtInfoTwo));
// 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData2);
                ToastUtil.makeText(context, "复制成功");
                break;
            case R.id.btn_three_copy:
                // 创建普通字符型ClipData
                ClipData mClipData3 = ClipData.newPlainText("Label", UI.toString(txtInfoThree));
// 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData3);
                ToastUtil.makeText(context, "复制成功");
                break;
            case R.id.btn_phone:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + PreferenceUtils.getString(Application.getInstance(), "service_phone", "")));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                break;
            case R.id.btn_submit:
                if (onChooseListener != null) {
                    onChooseListener.onSubmit();
                }
                dismiss();
                break;
        }
    }

    private OnChooseListener onChooseListener;

    public PacketPayStepDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    public interface OnChooseListener {
        void onSubmit();
    }
}
